package com.bbshop.service.base.impl;

import java.util.LinkedHashMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bbshop.bean.common.Pager;
import com.bbshop.bean.common.QueryResult;
import com.bbshop.bean.common.SearchCondition;
import com.bbshop.dao.base.DAO;
import com.bbshop.service.base.BaseService;

@Transactional
@Service
public abstract class BaseServiceSupport<T> implements BaseService<T> {
	// @Resource(type = getGenericType(0))
//	@Resource
	protected DAO<T> dao;

	@Override
	public void save(T pt) {
		dao.save(pt);
	}

	public void update(Object entity) {

		dao.update(entity);

	}

	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	@Override
	public T find(Class<T> clazz, Object entityId) {
		// System.out.println(getGenericType(0));
		return dao.find(clazz, entityId);
	}

	// private Class getGenericType(int index) {
	// Type genType = getClass().getGenericSuperclass();
	// if (!(genType instanceof ParameterizedType)) {
	// return Object.class;
	// }
	// Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
	// if (index >= params.length || index < 0) {
	// throw new RuntimeException("Index outof bounds");
	// }
	// if (!(params[index] instanceof Class)) {
	// return Object.class;
	// }
	// return (Class) params[index];
	// }

	@Override
	public T delete(Class<T> clazz, Object entityId) {
		T pt = find(clazz, entityId);
		dao.delete(clazz, entityId);
		return pt;
	}

	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	@Override
	public QueryResult<T> getScrollData(Class<T> clazz, int firstindex,
			int maxresult, LinkedHashMap<String, String> orderby) {
		QueryResult<T> qr = dao.getScrollData(clazz, firstindex, maxresult,
				orderby);
		return qr;

	}

	@Override
	@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED)
	public QueryResult<T> getScrollData(Class<T> clazz, Pager pager,
			SearchCondition<T> sc) {
		return dao.getScrollData(clazz, pager, sc);
	}
}
